package com.example;

import java.sql.*;

public class PeopleJDBC {

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/servlet";
            Connection connection = DriverManager.getConnection(url,
                    "root",
                    "123456");
            Statement statement = connection.createStatement();
            String sql = "select * from `people`";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                // 获取每一条数据的id
                long id = resultSet.getLong("id");
                // 获取每一条数据的username
                String username = resultSet.getString("username");
                System.out.println(id + " " + username);
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
